home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / PCalcSphere_Slow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  5.7 KB  |  213 lines

  1. /* SAS/C: sc PCalcSphere_Slow.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning sphere consisting entirely of dots.  The
  4. ** object is pre-calculated into a set of animations.
  5. **
  6. ** WARNING: This pre-calculated version has 360 frames of animation!
  7. ** It requires * 1.5 MEGS * to run, so make sure you have enough memory, and
  8. ** maybe make a cup of coffee while waiting for it to generate the anim...
  9. ** If you want it to run faster, get rid of the floating math and replace it
  10. ** with integers.
  11. **
  12. ** If you have a lot of memory and don't mind waiting, try increasing the
  13. ** amount of dots to a larger number.
  14. */
  15.  
  16. #include <proto/dpkernel.h>
  17. #include <system/debug.h>
  18. #include <math.h>
  19.  
  20. BYTE *ProgName      = "Spinning Sphere";
  21. BYTE *ProgAuthor    = "Paul Manias";
  22. BYTE *ProgDate      = "February 1998";
  23. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  24. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  25.  
  26. struct GScreen *screen;
  27. struct JoyData *jport;
  28.  
  29. void Demo(void);
  30. void CalcFrames(void);
  31.  
  32. #define AMTDOTS 500          /* The amount of dots in the object. */
  33. #define FRAMES  360          /* How many frames? [1280,640,360,180,90,45] */
  34. #define ANGLE   (360/FRAMES) /* Angle/Speed is dependant on the amount of frames */
  35. #define SCALE   80           /* Increase the scale to make the object larger */
  36.  
  37. struct DotPixel { double X,Y,Z; };
  38.  
  39. LONG palette[] = {
  40.   PALETTE_ARRAY,32,
  41.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  42.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  43.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  44.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  45. };
  46.  
  47. struct PixelEntry *lists[FRAMES];
  48.  
  49. struct PixelList PixelList = {
  50.   AMTDOTS,
  51.   sizeof(struct PixelEntry),
  52.   NULL
  53. };
  54.  
  55. /***********************************************************************************/
  56.  
  57. void main(void)
  58. {
  59.   WORD i;
  60.  
  61.   if (screen = InitTags(NULL,
  62.      TAGS_SCREEN,    NULL,
  63.      GSA_Attrib,     SCR_DBLBUFFER,
  64.        GSA_BitmapTags, NULL,
  65.        BMA_Palette,    palette,
  66.        TAGEND, NULL,
  67.      TAGEND)) {
  68.  
  69.      if (jport = Init(Get(ID_JOYDATA),NULL)) {
  70.         for (i=0; i < FRAMES; i++) lists[i] = NULL; /* Clear the array */
  71.  
  72.         for (i=0; i < FRAMES; i++) {
  73.            if (!(lists[i] = AllocMemBlock(sizeof(struct PixelEntry)*AMTDOTS,MEM_DATA)))
  74.               goto free; /* Nasty break */
  75.         }
  76.         PixelList.Pixels = lists[i];
  77.  
  78.         CalcFrames();
  79.  
  80.         Demo();
  81.  
  82. free:   for (i=0; i < FRAMES; i++) {
  83.            FreeMemBlock(lists[i]);
  84.         }
  85.  
  86.      Free(jport);
  87.      }
  88.   Free(screen);
  89.   }
  90. }
  91.  
  92. /************************************************************************************
  93. **
  94. */
  95.  
  96. void Demo(void)
  97. {
  98.   WORD i=NULL;
  99.  
  100.   Display(screen);
  101.  
  102.   do {
  103.     Query(jport);
  104.     Clear(screen->Bitmap);
  105.  
  106.     PixelList.Pixels = lists[i];
  107.  
  108.     DrawPixelList(screen->Bitmap,&PixelList);
  109.  
  110.     i++;
  111.     if (i >= FRAMES) {
  112.        i = NULL;
  113.     }
  114.  
  115.     WaitAVBL();
  116.     SwapBuffers(screen);
  117.   } while(!(jport->Buttons & JD_LMB));
  118. }
  119.  
  120. /************************************************************************************
  121. ** Longtitude deterimines the position of the dot on the horizontal axis.
  122. ** Latitude determines the position of the dot on the vertical axis.
  123. */
  124.  
  125. void CalcFrames(void)
  126. {
  127.   struct DotPixel *object; /* Pointer to our 3D object */
  128.   double *sine;            /* Pointer to our sine table */
  129.   double *cosine;         /* Pointer to our cosine table */
  130.   register WORD   i,j,t;
  131.   WORD   offsetx = (screen->Width/2);
  132.   WORD   offsety = (screen->Height/2);
  133.   double angle   = 0;
  134.   LONG   scale   = SCALE;
  135.   UWORD  anglex  = 0, angley = 0, anglez = 0;
  136.   double temp;
  137.   ULONG  colour;
  138.   double Z2,X2,Y2;
  139.   double longtitude;
  140.   double latitude;
  141.   struct PixelEntry *Entry;
  142.  
  143.   DMsg("Allocating 3D calculation memory.");
  144.  
  145.   if (object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA)) {
  146.    if (sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA)) {
  147.     if (cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA)) {
  148.  
  149.      /* First calculate the X, Y and Z coordinates of our object */
  150.  
  151.      DMsg("Calculating the object's coordinates.");
  152.  
  153.      for (i = 0; i < AMTDOTS; i++) {
  154.        longtitude = FastRandom(100);
  155.        latitude   = FastRandom(100);
  156.        object[i].X  = sin(longtitude)*cos(latitude);
  157.        object[i].Y  = sin(longtitude)*sin(latitude);
  158.        object[i].Z  = cos(longtitude);
  159.      }
  160.  
  161.      DMsg("Generating sine and cosine tables.");
  162.  
  163.      /* Now generate our cosine and sinus tables */
  164.  
  165.      for (i = 0; i < 360; i++) {
  166.        cosine[i] = cos(angle);
  167.        sine[i]   = sin(angle);
  168.        angle    += 0.25;
  169.      }
  170.  
  171.      /* Do our big pre-calculation loop */
  172.  
  173.      DMsg("Big calculation loop is starting now...");
  174.  
  175.      for (j = 0; j < FRAMES; j++) {
  176.  
  177.        Entry = lists[j];
  178.  
  179.        DPrintF("Frame:","%d",j);
  180.  
  181.        for (i = 0; i < AMTDOTS; i++) {
  182.          X2 = object[i].X;
  183.          Y2 = object[i].Y;
  184.          Z2 = object[i].Z;
  185.   
  186.          temp = Z2; /* X axis */
  187.          Z2 = (Z2 * cosine[anglex]) - (Y2 * sine[anglex]);
  188.          Y2 = (Y2 * cosine[anglex]) + (temp * sine[anglex]);
  189.  
  190.          temp = Z2; /* Y axis */
  191.          Z2 = (Z2 * cosine[angley]) - (X2 * sine[angley]);
  192.          X2 = (X2 * cosine[angley]) + (temp * sine[angley]);
  193.  
  194.          colour = ((Z2+1)*screen->Bitmap->AmtColours)/2;
  195.  
  196.          Entry->XCoord = (X2*scale)+offsetx;
  197.          Entry->YCoord = (Y2*scale)+offsety;
  198.          Entry->Colour = colour;
  199.          Entry++;
  200.        }
  201.        anglex += ANGLE; if (anglex >= 360) anglex = 0;
  202.        angley += ANGLE; if (angley >= 360) angley = 0;
  203.        anglez += ANGLE; if (anglez >= 360) anglez = 0;
  204.      }
  205.     FreeMemBlock(cosine);
  206.     }
  207.    FreeMemBlock(sine);
  208.    }
  209.   FreeMemBlock(object);
  210.   }
  211. }
  212.  
  213.